package nl.vu.few.ibivu.biomodels.client.ui;

import nl.vu.few.ibivu.biomodels.client.place.MyModelsBrowsingPlace;
import nl.vu.few.ibivu.biomodels.client.place.ModelPlace;
import nl.vu.few.ibivu.biomodels.client.place.PreferencesViewPlace;
import nl.vu.few.ibivu.biomodels.client.place.WelcomePlace;
import nl.vu.few.ibivu.biomodels.client.ui.base.OverMenuButton;
import nl.vu.few.ibivu.biomodels.client.ui.view.MenuBarView;

import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;
import com.smartgwt.client.widgets.toolbar.ToolStripMenuButton;

public class MenuBarViewImpl extends ToolStrip implements MenuBarView {

	Presenter presenter;
	
	MenuItem newModelItem;
	MenuItem saveAsItem;
	MenuItem saveModelItem;
	MenuItem executeModelItem;
	MenuItem exportModelItem;
	Menu modelMenu;
	
	ToolStripButton closeButton;
	ToolStripButton modelBrowsingButton;
	ToolStripButton preferencesButton;

	public MenuBarViewImpl() {
		super();
		
		this.presenter = null;
		
		this.setMembersMargin(30);
		this.setWidth100();
		this.setHeight(40);
		this.setStyleName("menuBar");
		
		modelMenu = new Menu();
		modelMenu.setShowShadow(true);
		
		newModelItem = new MenuItem("New", "[SKIN]add.png");
		newModelItem.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(MenuItemClickEvent event) {
				presenter.goTo(new ModelPlace(null));
			}
			
		});
		
		// create the Model Menu item: Save As
		saveAsItem = new MenuItem("Save As...");
		
		// create the Model Menu item: Save
		saveModelItem = new MenuItem("Save");
		saveModelItem.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(MenuItemClickEvent event) {
				presenter.requestModelSave();
			}
			
		});
		
		// create the Model Menu item: Execute
		executeModelItem = new MenuItem("Execute");
		executeModelItem.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(MenuItemClickEvent event) {
				presenter.requestModelExecute();
			}
			
		});
		
		// create the Model Menu item: Export
		exportModelItem = new MenuItem("Export");
		
		// wiring all the Model Menu items together on a ToolStripMenuButton
		modelMenu.setItems(newModelItem, saveModelItem, saveAsItem, executeModelItem, exportModelItem);	
		ToolStripMenuButton menuButton = new OverMenuButton("Model", modelMenu);
		this.addMember(menuButton);
		
		/*
		 * My Models Button
		 */
		modelBrowsingButton = new ToolStripButton();  
		modelBrowsingButton.setTitle("My Models");
		modelBrowsingButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				presenter.goTo(new MyModelsBrowsingPlace());
			}
		});
		this.addButton(modelBrowsingButton);
		
		/*
		 * Preferences Button
		 */
		preferencesButton = new ToolStripButton();
		preferencesButton.setTitle("Preferences");
		preferencesButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				presenter.goTo(new PreferencesViewPlace());
			}
		});
		this.addButton(preferencesButton);
		
		/*
		 * Close Button
		 */
		closeButton = new ToolStripButton();  
		closeButton.setTitle("Close");
		closeButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				presenter.goTo(new WelcomePlace());
			}
		});
		this.addFill();
		this.addButton(closeButton);
	}

	@Override
	public void setCanNewModel(boolean value) {
		newModelItem.setEnabled(value);
		
	}

	@Override
	public void setCanCloseModel(boolean value) {
		closeButton.setVisible(value);
		
	}

	@Override
	public void setCanSaveModel(boolean value) {
		saveModelItem.setEnabled(value);
	}

	@Override
	public void setCanSaveAsModel(boolean value) {
		saveAsItem.setEnabled(value);
		
	}

	@Override
	public void setCanExportModel(boolean value) {
		exportModelItem.setEnabled(value);
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void setCanBrowseModels(boolean value) {
		modelBrowsingButton.setVisible(value);
		
	}
	
	@Override
	public void setCanViewPreferences(boolean value) {
		preferencesButton.setVisible(value);
	}
	
	@Override
	public void setCanExecuteModel(boolean value) {
		executeModelItem.setEnabled(value);	
	}

}
